﻿using AutoMapper;
using PagedList;
using SIG.DAL.Dapper.Model;
using SIG.DAL.Dapper.Repositories;
using SIG.Infrastructure.Configs;
using SIG.Service;
using SIG.ViewModel.Front.Articles;
using SIG.WebMVC.Filters;
using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;

namespace SIG.WebMVC.Controllers
{
    [RoutePrefix("news")]
    public class ArticleController : BaseController
    {

        private readonly ArticleCategoryRepository _categoryService;
        private readonly ArticleRepository _articleService;
        private readonly PageMetaRepository _pageMetaService;
        private readonly IMapper _mapper;

        public ArticleController(IMapper mapper)
        {
            _categoryService = new ArticleCategoryRepository();
            _articleService = new ArticleRepository() ;
            _pageMetaService = new PageMetaRepository();
            _mapper = mapper;

        }

        // GET: Article
        [Route]
        [SIGActionFilter]
        public ActionResult Index(int? page, /*int? categoryId,*/ string keyword)
        {
            var vm = new ArticleListVM();

            //if (categoryId != null)
            //    vm.CategoryId = (int)categoryId;

            vm.Keyword = keyword;
            vm.PageIndex = page ?? 1;
            vm.PageSize = SettingsManager.Article.FrontPageSize;
            int totalCount;
            var goodslist = _articleService.GetListPagedAcitveItems(vm.PageIndex, vm.PageSize, 1, vm.Keyword,out totalCount);
            //var categoryVMList = _mapper.Map<List<Article>, List<ArticleVM>>(goodslist);
            vm.TotalCount = totalCount;

            vm.Articles = new StaticPagedList<Article>(goodslist, vm.PageIndex, vm.PageSize, vm.TotalCount); ;

            var categoryList = _categoryService.GetActiveItems().OrderByDescending(c => c.Importance).ToList();
            var categories = new SelectList(categoryList, "Id", "Title");
            ViewBag.Categories = categories;

            var url = Request.RawUrl;
            vm.PageMeta = _pageMetaService.GetPageMeta(ModelType.MENU, url);

            return View(vm);

        }

        [HttpGet]
        [Route("business")]
        public ActionResult Business()
        {
            var pageIndex = 1;
            int pageSize = int.MaxValue;
            int totalCount;
            var category = _categoryService.GetItemBySeoName("business");
            var articleList = _articleService.GetListPagedAcitveItems(pageIndex, pageSize, category.Id, string.Empty, out totalCount);
            //var vm = _mapper.Map<List<Article>, List<ArticleVM>>(articleList);
            return View(articleList);
        }


        [HttpGet]
        [Route("business/{id}")]
        public ActionResult BusinessDetail(int id)
        {
            var article = _articleService.GetById(id);
            article.ViewCount++;
            _articleService.Update(article);

            ViewBag.PageMeta = _pageMetaService.GetPageMeta(ModelType.ARTICLE, id.ToString());

            return View(article);
        }


        [HttpGet]
        [Route("list-{seoname}")]
        public ActionResult ListByCategory(string seoname)
        {
            var pageIndex = 1;
            int pageSize = SettingsManager.Article.PageSize;
            int totalCount;
            var category = _categoryService.GetItemBySeoName(seoname);
            var articleList = _articleService.GetListPagedAcitveItems(pageIndex, pageSize, category.Id, string.Empty, out totalCount);
            //var vm = _mapper.Map<List<Article>, List<ArticleVM>>(articleList);

            return View(articleList);

        }


        public PartialViewResult LatesterArticles(string seoname, int count)
        {
            var category = _categoryService.GetItemBySeoName(seoname);
            var articlelist = _articleService.GetListPagedAcitveItems(1,count, category.Id);
            //var vm = _mapper.Map<List<Article>, List<ArticleVM>>(articlelist);

            return PartialView("_LatesterArticles", articlelist);
        }


        [HttpGet]

        public ActionResult Detail(int id)
        {
            var article = _articleService.GetById(id);
            article.ViewCount++;
            _articleService.Update(article);

            ViewBag.PageMeta = _pageMetaService.GetPageMeta(ModelType.ARTICLE, id.ToString());

            return View(article);
        }

        public PartialViewResult ShowArticlesInHome(string seoname, int count)
        {
            int reCount;
            var category = _categoryService.GetItemBySeoName(seoname);
            var categoryId = category != null ? category.Id : 0;
          
            var list = _articleService.GetListPagedRecommendItems(1, count, categoryId, string.Empty, out reCount);                
            return PartialView("_ShowArticlesInHome", list);
        }
    }
}